<?php
/**
* Demands modeller
* @author: trungdt
* @since: Feb 2, 2011
* 
* Change logs
* Feb 2, 2011 - trungdt - First create
* Feb 8, 2011 - trungdt - Add witness form
*/
class demands {
    
    /**
    * Generate demand edit form for normal user
    * @return form
    */
	static function form($object) {
		if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
		exponent_forms_initialize();
		
		global $db;
		
        // load language
        $ilang=exponent_lang_loadFile("modules/demandmodule/demand.php");
        
        // prayer type
        $prayer_type=array();
        $prayer_type[0]=$ilang['prayer_type_all'];
        $prayer_type[1]=$ilang['prayer_type_member'];
        $prayer_type[2]=$ilang['prayer_type_staff'];
        
        // subject list
        $subject=$db->selectDropdown("subjects","name");
        
		$form = new form();
        
		if (!isset($object->id)) {
            // for normal user, we just create enough field
			$object->name = '';
            $object->content = '';
            
            // get subjects list
            foreach ($subject as $key=>$value) {
                $object->subject_id=$key;
                break;
            }
            
            // prayer type
            $object->prayer_type = 0; // default is 0
		}
		
        $form->register('name',$ilang['demand_title'],new textcontrol($object->name,50,false,100,"",true));
        $form->register('content',$ilang['demand_content'],new texteditorcontrol($object->content,10,80));
        $form->register('subject_id',$ilang['demand_subject'],new dropdowncontrol($object->subject_id, $subject));
        $form->register('prayer_type',$ilang['demand_prayer_type'],new dropdowncontrol($object->prayer_type, $prayer_type));
        
        demands::_demand_edit_form($form, $object);
        
        if (isset($object->id))
        {
            $form->meta('id',$object->id);
            demands::_demand_edit_form($form, $object);
        }
        else
        {
            // we must load capcha for post new here
            if (EXPONENT_HAS_GD){
                $form->register('capcha','',new htmlcontrol(sprintf($ilang['captcha_description'],'<img src="'.PATH_RELATIVE.'captcha.php" />')));
                $form->register('captcha_string','',new textcontrol('',6));
            }
            else
            {
                // fall back if there is not GD Extensition
                flash("error","Please install GD extensition");
                header("Location: /");
            }
        }
        
		$form->register('submit','',new buttongroupcontrol($ilang['save'],'',$ilang['cancel']));
		return $form;
	}
	
    /**
    * Generate demand edit form for advance user
    * @return form
    */
    static function _demand_edit_form(&$form, $object)
    {
      $loc=null;
      $loc->mod="demandmodule";  
      if (exponent_permissions_check("manage",$loc) 
            || exponent_permissions_check("approve_demand",$loc))
      {
           if (isset($object->id))
           {
               $form->register('publish_date',"Ngày có hiệu lực",new yuidatetimecontrol($object->publish_date,"Ngay lập tức", true, false, false, true));
               if ($object->unpublish_date==0)
               {
                   $object->unpublish_date=strtotime("+10 days");
               }
               $form->register('unpublish_date',"Ngày hết hiệu lực",new yuidatetimecontrol($object->unpublish_date,"Sau 10 ngày", true, false, false, true));
               // approve
               $approve=array();
               $approve[0]="Chờ duyệt";
               $approve[1]="Đã duyệt và không đồng ý";
               $approve[2]="Đã duyệt và đồng ý";
               $form->register('status',"Trạng thái",new dropdowncontrol($object->status,$approve));
               $form->register('unapprove_reason','Lý do không duyệt',new textcontrol($object->unapprove_reason,50,false,200));
               
               // important level
               $impo=array();
               $impo[1]="Cá nhân";
               $impo[2]="Quan trọng";
               $impo[3]="Rất quan trọng";
               $impo[4]="Khẩn cấp";
               $impo[5]="Hội thánh";
               $impo[6]="Khác";
               $form->register('important_level',"Mức quan trọng",new dropdowncontrol($object->important_level,$impo));
               
               // sendmail
               $temp=new checkboxcontrol($object->sendmail);
               $temp->flip=true;
               $form->register('sendmail','&nbsp;Email tin này đến các thành viên',$temp);
               
               // ring list
               global $db;
               $ring_list=$db->selectObjects("pray_ring");
               $rings=array();
               $default_ring=-1;
               foreach ($ring_list as $k=>$v)
               {
                   if ($v->default_ring == 1)
                   {
                       $default_ring=$v->id;
                   }
                   $rings[$v->id]=$v->name;
               }
               if ($object->ring_id == -1)
               {
                   $object->ring_id=$default_ring;
               }
               $form->register('ring_id',"Vòng cầu nguyện",new dropdowncontrol($object->ring_id,$rings));
           }
      }
    }
    
    /**
    * Demand update, for advance user
    * 
    */
	static function _demand_update($values,&$object) {
        global $db, $user;
        // check again, can we continue (or this record is exist or not)
        $object->id=intval($object->id);
        $temp=$db->selectObject("demands","id = {$object->id}");
        if ($temp != null)
        {    
            $loc=null;
            $loc->mod="demandmodule";  
            if (exponent_permissions_check("manage",$loc) 
            || exponent_permissions_check("approve_demand",$loc))
            {
                // okies, we can continue update object   
                $object->publish_date = yuidatetimecontrol::parseData('publish_date',$values);
                if ($object->publish_date==0) // publish immediately
                {
                    $object->publish_date=time();
                }
                $object->unpublish_date = yuidatetimecontrol::parseData('unpublish_date',$values);
                if ($object->unpublish_date==0) // publish immediately
                {
                    $object->unpublish_date=strtotime("+10 days");
                }
                $object->status=intval($values['status']);
                $object->unapprove_reason=strip_tags($values['unapprove_reason']);
                $object->important_level=intval($values['important_level']);
                $object->unapprove_username=$user->username;
                
                if (isset($values['sendmail']))
                {
                    $object->sendmail=1;
                }
                else
                {
                    $object->sendmail=0;
                }
                
                // ring
                $object->ring_id=intval($values['ring_id']);
            }
        }
    }
    
    static function update($values,$object) {
        global $user;
        
        $object->subject_id = intval($values['subject_id']);
        $object->name=htmlspecialchars($values['name'], ENT_QUOTES);
        $object->content=htmlspecialchars($values['content'], ENT_QUOTES);
		$object->prayer_type = $values['prayer_type'];
        
        // we calculate other value
        if (isset($object->id))
        {
            demands::_demand_update($values, $object);
        }else{
            $object->user_id=$user->id;
            $object->username=$user->username;
            $object->post_date=time();
        }
        
		return $object;
	}
    
    /**
    * Add advance form element for advance user, for witness form
    * 
    */
    function witness_advance_form($object, &$form)
    {
        global $db;
        $loc=null;
        $loc->mod="demandmodule";
        if (isset($object->id) &&
            (exponent_permissions_check("manage",$loc) 
           || exponent_permissions_check("approve_demand",$loc)))
        {
            
                $subject=$db->selectDropdown("subjects","name");
                if (!($object->subject_id>0))
                {   
                    // get subjects list
                    foreach ($subject as $key=>$value) {
                        $object->subject_id=$key;
                        break;
                    }
                }
                $form->register('subject_id',"Chủ đề cầu nguyện",new dropdowncontrol($object->subject_id, $subject));
                
               $form->register('publish_date',"Ngày có hiệu lực",new yuidatetimecontrol($object->publish_date,"Ngay lập tức", true, false, false, true));
               // approve
               $approve=array();
               $approve[0]="Chờ duyệt";
               $approve[1]="Đã duyệt và không đồng ý";
               $approve[2]="Đã duyệt và đồng ý";
               $form->register('status',"Trạng thái",new dropdowncontrol($object->status,$approve));
               $form->register('unapprove_reason','Lý do không duyệt',new textcontrol($object->unapprove_reason,50,false,200));
        }
    }
    
    /**
    * Witness form
    * @author: trungdt
    * @since: Feb 8, 2011
    * @version: 1.0
    * @return: form
    */
    static function witness_form($object) {
        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();
        
        global $db;
        if (!isset($object->id))
        {
            $object->name="";
            $object->content="";
        }
        
        // load language
        $ilang=exponent_lang_loadFile("modules/demandmodule/demand.php");
        
        $form = new form();
        
        
        $form->register('name',$ilang['witness_name'],new textcontrol($object->name,50,false,100,"",true));
        $form->register('content',$ilang['witness_content'],new texteditorcontrol($object->content,10,80));
        if (isset($object->id))
        {
            $form->meta('id',$object->id);
            demands::witness_advance_form($object, $form);
        }
        else
        {
            // we must load capcha for post new here
            if (EXPONENT_HAS_GD){
                $form->register('capcha','',new htmlcontrol(sprintf($ilang['captcha_description'],'<img src="'.PATH_RELATIVE.'captcha.php" style="float: none;"/>')));
                $form->register('captcha_string','',new textcontrol('',6));
            }
            else
            {
                // fall back if there is not GD Extensition
                die();
            }
        }
        $form->register('submit','',new buttongroupcontrol($ilang['save'],'',""));
        return $form;
    }
    
    /**
    * Update witness
    * @author: trungdt
    * @since: Feb 9, 2011
    * @version: 1.0
    * 
    */
    static function update_witness($values,$demand, $object) {
        global $user;
        
        $object->name=$values['name'];
        $object->content=htmlspecialchars($values['content'], ENT_QUOTES);
        $object->username=$user->username;
        if (isset($object->id)) // update witness
        {
            if (isset($values['status']))
            {
                $object->status=intval($values['status']);
            }
            if (isset($values['unapprove_reason']))
            {
                $object->unapprove_reason=$values['unapprove_reason'];
            }
            $object->publish_date = yuidatetimecontrol::parseData('publish_date',$values);
            if ($object->publish_date == 0)
            {
                $object->publish_date=time();
            }
            $object->unapprove_username=$user->username;
            if (isset($values['subject_id']))
            {
                $object->subject_id=intval($user->subject_id);
            }
        }
        else
        {
            $object->user_id=$user->id;
            $object->demand_id = $demand->id;
            $object->post_date = time();
            $object->status=0; // add new: must in waiting
        }
        return $object;
    }
}

?>